package cn.edu.hqu.javaee.web;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import cn.edu.hqu.javaee.domain.Choose;
import cn.edu.hqu.javaee.domain.Course;
import cn.edu.hqu.javaee.domain.Personal;
import cn.edu.hqu.javaee.domain.User;
import cn.edu.hqu.javaee.domain.Course.Type;
import cn.edu.hqu.javaee.repository.ChooseRepository;
import cn.edu.hqu.javaee.repository.CourseRepository;
import cn.edu.hqu.javaee.repository.UserRepository;


@Controller
@RequestMapping("/choose")
@SessionAttributes("personal")//只能作用在类上，作用是将指定的Model中的键值对添加至session中，方便在下一次请求中使用。
public class ChooseController {
	private final CourseRepository courseRepo;
	private ChooseRepository chooseRepo;
	private UserRepository userRepo;
	 @Autowired
	  public ChooseController(CourseRepository courseRepo,ChooseRepository chooseRepo,UserRepository userRepo) {
	    this.courseRepo = courseRepo;
	    this.chooseRepo = chooseRepo;
	    this.userRepo=userRepo;
	  }
	 @ModelAttribute(name = "personal")
	  public Personal personal() {
	    return new Personal();
	  }
	  
	  @ModelAttribute(name = "choose")
	  public Choose choose() {
	    return new Choose();
	  }
	  
	
	  @GetMapping
	  public String showChoose(Model model) {
	    List<Course> courses = new ArrayList<>();
	    courseRepo.findAll().forEach(i -> courses.add(i));
	    
	    Type[] types = Course.Type.values();
	    for (Type type : types) {
	      System.out.println(type.toString().toLowerCase());
	      System.out.println(filterByType(courses, type));
	      model.addAttribute(type.toString().toLowerCase(), 
	          filterByType(courses, type));      
	    }
	    List<User> user = new ArrayList<>();
		userRepo.findAll().forEach(i -> user.add(i));
		model.addAttribute("user",user);
		System.out.println(user);
	    return "choose";
	  }
	  @PostMapping
	  public String processChoose(@Valid Choose choose, Errors errors, @ModelAttribute Personal personal){
		  if (errors.hasErrors()) {
			return "choose";
		}
	    Choose saved = chooseRepo.save(choose);
	    personal.addDesign(saved);
	    System.out.println("choose"+choose);
	    System.out.println("personal"+personal);
	    return "redirect:/checkout/personal";
	  }
	  private List<Course> filterByType(
		      List<Course> courses, Type type) {
		    return courses
		              .stream()
		              .filter(x -> x.getType().equals(type))
		              .collect(Collectors.toList());
		  }
}
